<html xmlns="http://www.w3.org/1999/xhtml" manifest="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback.cacheManifest">
<head>
<title>Fallback entry test</title>

<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/dom/tests/mochitest/ajax/offline/offlineTests.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />

<script class="testbody" type="text/javascript">

/**
 * This tests that we fall back to the correct fallback entries when loading
 * from fallback namespaces listed in the manifest.  The test is performed twice
 * to make sure that no cache entries are created for the nonexistent items."
 * That would lead to a failure to fallback, as there would always be an entry
 * in the application cache.
 */

var gStep = 1;
var gChildLoad = false;
var gTopWindow = null;

function manifestUpdated()
{
  fallbackFrame.location = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/non-existing.html";
  // Invokes load of fallback.html
}

function onFallbackLoad(fallbackIdentification)
{
  switch (gStep)
  {
    case 001:
      OfflineTest.ok(!gChildLoad, "offline child not load before cache update");
      gChildLoad = true;
      // no break
    case 101:
      OfflineTest.is(fallbackIdentification, 1, "fallback for namespace1/ in step " + gStep);

      fallbackFrame.location = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/sub-non-existing.html";
      // Invokes load of fallback.html
      break;

    case 002:
    case 102:
      OfflineTest.is(fallbackIdentification, 1, "fallback for namespace1/, sub namespace in name of the frame in step " + gStep);

      fallbackFrame.location = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/sub/non-existing.html";
      // Invokes load of fallback2.html
      break;

    case 003:
    case 103:
      OfflineTest.is(fallbackIdentification, 2, "fallback for namespace1/sub/ in step " + gStep);

      fallbackFrame.location = "HTTP://MOCHI.TEST:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/sub/non-existing.html";
      // Invokes load of fallback2.html
      break;

    case 004:
    case 104:
      OfflineTest.is(fallbackIdentification, 2, "fallback for namespace1/sub/ in step " + gStep);

      // Try opening a non-existing page as a top level document. It must
      // fall to fallbackTop.html that identifies it self as '100'.
      // Invokes load of fallbackTop.html
      gTopWindow = window.open("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace2/non-existing.html");

      break;

    case 005:
      // Try all over again. This checks there are no entries for non-existing
      // pages created/leaked. That would prevent fallback load.
      gStep = 100;

      fallbackFrame.location = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/non-existing.html";
      // Invokes load of fallback1.html, again, from the start
      // no break
    case 105:
      OfflineTest.is(fallbackIdentification, 100, "fallback for namespace2/, invoked by a top level document " + gStep);

      gTopWindow.close();
      gTopWindow = null;

      break;
  }

  if (gStep == 105) {
    finalize();
    return;
  }

  ++gStep;
}


function finishTest()
{
  OfflineTest.teardownAndFinish();
}

function finalize()
{
  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

  var entries = [
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/sub-non-existing.html", false],
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/sub/non-existing.html", false],
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/non-existing.html", false],
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace2/non-existing.html", false]
  ];
  OfflineTest.checkCacheEntries(entries, finishTest);
}

SimpleTest.waitForExplicitFinish();

if (OfflineTest.setup()) {
  applicationCache.onerror = OfflineTest.failEvent;
  applicationCache.onupdateready = OfflineTest.failEvent;
  applicationCache.oncached = OfflineTest.priv(manifestUpdated);
}

</script>

</head>

<body>
<iframe name="fallbackFrame" src=""></iframe>
</body>
</html>
